package com.fancy.learncenter.ui.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ListView;

/**
 * Created by Administrator on 2017/7/6.
 */

public class MyTouchListView extends ListView {

    private int tempAnimatorValues = 0;
    private int nFirstShow;
    private ValueAnimator mAnimator;
    private OnListViewChange mOnListViewChange;
    private boolean isMove = false;

    public OnListViewChange getmOnListViewChange() {
        return mOnListViewChange;
    }

    public void setmOnListViewChange(OnListViewChange mOnListViewChange) {
        this.mOnListViewChange = mOnListViewChange;
    }

    public MyTouchListView(Context context) {
        super(context);
        setDividerHeight(0);
    }

    public MyTouchListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setDividerHeight(0);
    }

    public MyTouchListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setDividerHeight(0);
    }

    public boolean isMove() {
        return isMove;
    }

    public void setMove(boolean move) {
        isMove = move;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        View currentView;
        int nPaddingTop;
        int nMoveTop, nViewHeight;
        final int action = ev.getAction();

        switch (ev.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                if (isMove()) {
                    return true;
                }
                if (mOnListViewChange != null) {
                    mOnListViewChange.onTouchDown();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (isMove()) {
                    return true;
                }
                if (mOnListViewChange != null) {
                    mOnListViewChange.onTouchMove(getFirstVisiblePosition());
                }
                break;
            case MotionEvent.ACTION_UP:
                if (isMove()) {
                    return true;
                }
                currentView = getChildAt(0);
                nPaddingTop = getPaddingTop();
                nMoveTop = currentView.getTop();
                nViewHeight = currentView.getHeight();
                // Log.e("TAG", "滑动:nPaddingTop=" + nPaddingTop + " nTop:" + nTop +
                // " nItemHeight:" + nItemHeight);
                nFirstShow = getFirstVisiblePosition();
                if (nPaddingTop - nMoveTop < nViewHeight / 2) {
                    nFirstShow = getFirstVisiblePosition();
                    changeItemViewLocation(nPaddingTop - nMoveTop);
                } else {
                    nFirstShow = getFirstVisiblePosition() + 1;
                    changeItemViewLocation(-(nViewHeight - (nPaddingTop - nMoveTop)));
                }
                if (mOnListViewChange != null) {
                    mOnListViewChange.onTOuchUp(nFirstShow);
                }

                return true;
        }
        return super.onTouchEvent(ev);
    }


    private void changeItemViewLocation(int moveDistance) {
        if (mAnimator != null) {
            mAnimator.cancel();
        }
        tempAnimatorValues = moveDistance;
        mAnimator = ValueAnimator.ofInt(new int[]{moveDistance, 0});
        mAnimator.setDuration(900L).setInterpolator(new AccelerateDecelerateInterpolator());
        mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int animationValues = ((Integer) (animation.getAnimatedValue())).intValue();
                int values = tempAnimatorValues - animationValues;
                onLoayout(values);
                tempAnimatorValues = animationValues;
            }
        });

        mAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (mOnListViewChange != null) {
                    mOnListViewChange.onAnimFinish(nFirstShow);
                }
            }
        });
        mAnimator.start();

    }


    public void onLoayout(int paramAnimation) {
        int i = getChildCount();
        for (int j = 0; j < i; j++) {
            View localView = getChildAt(j);
//            Log.e("TAG", "j=" + j + "int:" + paramAnimation + " top:" + localView.getTop());
            localView.layout(localView.getLeft(), paramAnimation + localView.getTop(), localView.getRight(), paramAnimation + localView.getBottom());
        }
        invalidate();
    }

    @Override
    protected void onDetachedFromWindow() {
        if (mAnimator != null) {
            mAnimator.cancel();
        }
        super.onDetachedFromWindow();
    }

    public interface OnListViewChange {

        void onTouchDown();

        void onTouchMove(int firstVisiblePosition);

        void onTOuchUp(int firstVisiblePosition);

        void onAnimFinish(int firstVisiblePosition);
    }
}
